package com.sdhs.costcontrol.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.sdhs.costcontrol.domain.GcControlSettlementPayment;
import com.sdhs.myproject.domain.GcMyprojectContractInformation;
import com.sdhs.myproject.domain.GcMyprojectProfitMarginMeasurement;
import com.sdhs.myproject.mapper.GcMyprojectProfitMarginMeasurementMapper;
import com.sdhs.myproject.service.IGcMyprojectContractInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.costcontrol.domain.GcControlFinalAccounts;
import com.sdhs.costcontrol.mapper.GcControlFinalAccountsMapper;
import com.sdhs.costcontrol.service.IGcControlFinalAccountsService;

/**
 * 项目决算Service业务层处理
 * 
 * @author wanglei
 * @date 2025-03-11
 */
@Service
public class GcControlFinalAccountsServiceImpl implements IGcControlFinalAccountsService 
{
    @Autowired
    private GcControlFinalAccountsMapper gcControlFinalAccountsMapper;
    
    @Autowired
    private IBusinessFileService businessFileService;

    @Autowired
    private IGcMyprojectContractInformationService myprojectContractInformationService;

    @Autowired
    private GcMyprojectProfitMarginMeasurementMapper gcMyprojectProfitMarginMeasurementMapper;
    /**
     * 查询项目决算
     * 
     * @param id 项目决算主键
     * @return 项目决算
     */
    @Override
    public GcControlFinalAccounts selectGcControlFinalAccountsById(Long id)
    {
    	GcControlFinalAccounts controlFinalAccounts = gcControlFinalAccountsMapper.selectGcControlFinalAccountsById(id);
    	List<BusinessFile> fileList = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GC_CONTROL_FINAL_ACCOUNTS.getCode(), String.valueOf(id));
    	controlFinalAccounts.setFileList(fileList);
    	return controlFinalAccounts;
    }

    /**
     * 查询项目决算列表
     * 
     * @param gcControlFinalAccounts 项目决算
     * @return 项目决算
     */
    @Override
    public List<GcControlFinalAccounts> selectGcControlFinalAccountsList(GcControlFinalAccounts gcControlFinalAccounts)
    {
        return gcControlFinalAccountsMapper.selectGcControlFinalAccountsList(gcControlFinalAccounts);
    }

    /**
     * 新增项目决算
     * 
     * @param gcControlFinalAccounts 项目决算
     * @return 结果
     */
    @Override
    public int insertGcControlFinalAccounts(GcControlFinalAccounts gcControlFinalAccounts)
    {
		gcControlFinalAccounts.setCreater(SecurityUtils.getUsername());
        gcControlFinalAccounts.setCreateTime(DateUtils.getNowDate());
        int result = gcControlFinalAccountsMapper.insertGcControlFinalAccounts(gcControlFinalAccounts);
        handler(gcControlFinalAccounts);
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_CONTROL_FINAL_ACCOUNTS.getCode(), String.valueOf(gcControlFinalAccounts.getId()), gcControlFinalAccounts.getFileList());
		return result;
    }

    /**
     * 修改项目决算
     * 
     * @param gcControlFinalAccounts 项目决算
     * @return 结果
     */
    @Override
    public int updateGcControlFinalAccounts(GcControlFinalAccounts gcControlFinalAccounts)
    {
		gcControlFinalAccounts.setUpdater(SecurityUtils.getUsername());
        gcControlFinalAccounts.setUpdateTime(DateUtils.getNowDate());
        int result = gcControlFinalAccountsMapper.updateGcControlFinalAccounts(gcControlFinalAccounts);
        handler(gcControlFinalAccounts);
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_CONTROL_FINAL_ACCOUNTS.getCode(), String.valueOf(gcControlFinalAccounts.getId()), gcControlFinalAccounts.getFileList());
		return result;
    }

    /**
     * 批量删除项目决算
     * 
     * @param ids 需要删除的项目决算主键
     * @return 结果
     */
    @Override
    public int deleteGcControlFinalAccountsByIds(Long[] ids)
    {
        return gcControlFinalAccountsMapper.deleteGcControlFinalAccountsByIds(ids);
    }

    /**
     * 删除项目决算信息
     * 
     * @param id 项目决算主键
     * @return 结果
     */
    @Override
    public int deleteGcControlFinalAccountsById(Long id)
    {
        return gcControlFinalAccountsMapper.deleteGcControlFinalAccountsById(id);
    }
    public void handler(GcControlFinalAccounts gcControlFinalAccounts) {
        GcMyprojectProfitMarginMeasurement gcMyprojectProfitMarginMeasurement = new GcMyprojectProfitMarginMeasurement();
        gcMyprojectProfitMarginMeasurement.setProjectId(gcControlFinalAccounts.getProjectId());
        /*获取结算值*/
        BigDecimal finalAccountsValue = gcControlFinalAccounts.getFinalAccountsValue();
        GcMyprojectContractInformation all = myprojectContractInformationService.getAll(new Long[]{gcControlFinalAccounts.getProjectId()});

        // 处理可能的 null 值，默认给 0
        BigDecimal managementMoney = all.getManagementMoney() != null ? all.getManagementMoney() : BigDecimal.ZERO; // 成本控制总数
        BigDecimal totalPayMoney = all.getTotalPayMoney() != null ? all.getTotalPayMoney() : BigDecimal.ZERO; // 累计支付金额

        // 计算 moneyOver = ((settlementValue - managementMoney - totalPayMoney) / settlementValue) * 100
        BigDecimal moneyOver = finalAccountsValue
                .subtract(managementMoney)
                .subtract(totalPayMoney)
                .divide(finalAccountsValue, 10, RoundingMode.HALF_UP)  // 防止除零，并设置精度
                .multiply(new BigDecimal(100))
                .setScale(2, RoundingMode.HALF_UP);  // 保留2位小数并四舍五入

        //竣工利润率
        gcMyprojectProfitMarginMeasurement.setCompletionUtilization(moneyOver.toString()); //决算值
        gcMyprojectProfitMarginMeasurementMapper.updateCompletionMeasurement(gcMyprojectProfitMarginMeasurement);
    }
}
